package com.citrix.gs.util;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.dom4j.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.citrix.gs.util.beacon.BeaconCI;
import com.citrix.gs.util.beacon.BeaconJobType;
import com.citrix.gs.util.beacon.job.BeaconJob;
import com.citrix.gs.util.common.Config;
import com.offbytwo.jenkins.model.Job;

/** CI Performance checker
 * @author yuzho
 *
 */
public class PerfChecker {

    private static final Logger logger = LoggerFactory.getLogger(PerfChecker.class);
    private final BeaconCI ci;

    public PerfChecker(BeaconCI ci) {
        this.ci = ci;
    }
    
    public void run() {
        
        logger.info("Start to run all repositories on CI: {}", ci.getHostURL());
        
        //Get all top jobs
        try {
            List<Job> srcJobs = ci.getView(BeaconJobType.Src.name()).getJobs();
            List<Job> topJobs = ci.getTopJobs(srcJobs);
            ci.runJobs(topJobs);
            
        } catch (IOException e) {
            logger.error("", e);
        }
    }
    
    
    public static void main(String[] args) {
        
        // Run all jobs on Test CI
        PerfChecker perfChecker = new PerfChecker(new BeaconCI(Config.me().getTestCI()));
        perfChecker.run();
    }

}
